//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: Prefedit_cb.c 796 2007-06-30 19:00:01Z roger $
//

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gnome.h>
#include <glade/glade.h>
#include <gconf/gconf-client.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
//#include <gnome/global.h>
#include "prefedit.h"
#include "Prefedit_cb.h"
#include "SymbolNew_cb.h"
#include "IMAdd_cb.h"
#include "WordNew_cb.h"
#include "list_ctrl.h"
#include "list_factory.h"
#include "dialog.h"
#include "upimdpref.h"
#include "itabcpref.h"
#include "pinyinpref.h"
#include "wubipref.h"
//StatusModel * model = NULL;
//static char default_id;
typedef struct {
	gchar *font;
	gchar *fg;
	gchar *bg;
} AppearanceKeys;


static void
set_tc_sc_mode(int tc_sc_mode)
{
	GConfClient *client = gconf_client_get_default();
	gconf_client_set_int(client, TC_SC_KEY, tc_sc_mode, NULL);
}
//zzc
void
on_inTrayRadio_toggled                 (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	if (!gtk_toggle_button_get_active(togglebutton)) return;
	GConfClient *client = gconf_client_get_default();
	if (!gconf_client_get_bool(client, STATUS_IN_TRAY_KEY, NULL))
		gconf_client_set_bool(client, STATUS_IN_TRAY_KEY,TRUE, NULL);
}

void
on_notInTrayRadio_toggled                 (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	if (!gtk_toggle_button_get_active(togglebutton)) return;
	GConfClient *client = gconf_client_get_default();
	if (gconf_client_get_bool(client, STATUS_IN_TRAY_KEY, NULL))
		gconf_client_set_bool(client, STATUS_IN_TRAY_KEY, FALSE, NULL);
}

void
on_hzRadio_toggled                 (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	if (!gtk_toggle_button_get_active(togglebutton)) return;
	GConfClient *client = gconf_client_get_default();
	if (!gconf_client_get_bool(client, CANDIDATE_HORIZONTALLY_KEY, NULL))
		gconf_client_set_bool(client, CANDIDATE_HORIZONTALLY_KEY,TRUE, NULL);
}

void
on_vtRadio_toggled                 (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	if (!gtk_toggle_button_get_active(togglebutton)) return;
	GConfClient *client = gconf_client_get_default();
	if (gconf_client_get_bool(client, CANDIDATE_HORIZONTALLY_KEY, NULL))
		gconf_client_set_bool(client, CANDIDATE_HORIZONTALLY_KEY,FALSE, NULL);
}
//zzc

void
on_noConvRadio_toggled                 (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	if (!gtk_toggle_button_get_active(togglebutton)) return;
	set_tc_sc_mode(0);
}

void
on_tc2scRadio_toggled                  (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	if (!gtk_toggle_button_get_active(togglebutton)) return;
	set_tc_sc_mode(1);
}


void
on_sc2tcRadio_toggled                  (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	if (!gtk_toggle_button_get_active(togglebutton)) return;
	set_tc_sc_mode(2);
}

static const AppearanceKeys
get_current_keys(GtkMenuShell *menushell)
{
	GtkWidget *active_item;
	gint index;
	AppearanceKeys keys;
	active_item = gtk_menu_get_active(GTK_MENU(menushell));
	index = g_list_index(menushell->children, active_item);

	switch (index) {
		case 0:
			keys.font = PREEDIT_FONT_KEY;
			keys.fg = PREEDIT_FG_KEY;
			keys.bg = PREEDIT_BG_KEY;
			break;
		case 1:
			keys.font = CANDIDATE_FONT_KEY;
			keys.fg = CANDIDATE_FG_KEY;
			keys.bg = CANDIDATE_BG_KEY;
			break;
		case 2:
			keys.font = NUMBER_FONT_KEY;
			keys.fg = NUMBER_FG_KEY;
			keys.bg = NUMBER_BG_KEY;
			break;
		case 3:
			keys.font = STATUS_FONT_KEY;
			keys.fg = STATUS_FG_KEY;
			keys.bg = STATUS_BG_KEY;
			break;
	}

	return keys;
}

static AppearanceKeys current_keys = {
	PREEDIT_FONT_KEY, PREEDIT_FG_KEY, PREEDIT_BG_KEY
};
void
on_fontPicker_font_set                 (GnomeFontPicker *gnomefontpicker,
                                        gchar           *font_name,
                                        gpointer         user_data)
{
	GConfClient *client = gconf_client_get_default();
	gconf_client_set_string(client, current_keys.font, font_name, NULL);
}

void
on_componentOption_selected(GtkMenuShell *menushell, gpointer user_data)
{
	GladeXML *xml = (GladeXML *)user_data;
	GtkWidget *w;
	GConfClient *client;
	gchar *font, *colorstr;
	GdkColor color;
	
	current_keys = get_current_keys(menushell);

	client = gconf_client_get_default();
	font = gconf_client_get_string(client, current_keys.font, NULL);
	w = glade_xml_get_widget(xml, "fontPicker");
	gtk_font_button_set_font_name(GTK_FONT_BUTTON(w), font);
	g_free(font);

	colorstr = gconf_client_get_string(client, current_keys.fg, NULL);
        if (!colorstr || !gdk_color_parse(colorstr, &color)) {
		if (!strcmp(current_keys.fg, NUMBER_FG_KEY))
			gdk_color_parse("#40A536", &color);
		else
			gdk_color_parse("#000000", &color);
        }
        w = glade_xml_get_widget(xml, "fgColorPicker");
        gtk_color_button_set_color(GTK_COLOR_BUTTON(w), &color);
        g_free(colorstr);

	if ((!strcmp(current_keys.bg, PREEDIT_BG_KEY))
	    || (!strcmp(current_keys.bg, CANDIDATE_BG_KEY))) {
		w = glade_xml_get_widget(xml, "bgColorPickerLabel");
		gtk_widget_set_sensitive(w, TRUE);
		w = glade_xml_get_widget(xml, "bgColorPicker");
		gtk_widget_set_sensitive(w, TRUE);
	} else {
		w = glade_xml_get_widget(xml, "bgColorPickerLabel");
		gtk_widget_set_sensitive(w, FALSE);
		w = glade_xml_get_widget(xml, "bgColorPicker");
		gtk_widget_set_sensitive(w, FALSE);
	}
        colorstr = gconf_client_get_string(client, current_keys.bg, NULL);
        if (!colorstr || !gdk_color_parse(colorstr, &color)) {
                gdk_color_parse("#FFFFFF", &color);
        }
        gtk_color_button_set_color(GTK_COLOR_BUTTON(w), &color);
        g_free(colorstr);
}

void
on_defaultIMOption_selected(GtkComboBox *combobox, gpointer user_data)
{
	//GladeXML *xml = (GladeXML *)user_data;
	gint im_list_idx = gtk_combo_box_get_active(combobox);
	GConfClient *client = gconf_client_get_default();
	IMInfo *inf = (IMInfo *)g_list_nth_data(im_curr_list, im_list_idx);

	gconf_client_set_string(client, DEFAULT_IM_KEY, inf->id, NULL);
//	default_id=inf->id;
}

void
on_tc_to_sc_item_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	set_tc_sc_mode(1);
}

void
on_sc_to_tc_item_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	set_tc_sc_mode(2);
}	

void
on_no_conv_item_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	set_tc_sc_mode(0);
}

static void
real_color_set(const gchar *key, guint r, guint g, guint b, guint a)
{
	GConfClient *client;
	gchar *colorstr;

	colorstr = g_strdup_printf("#%02X%02X%02X", r/256, g/256, b/256);
	client = gconf_client_get_default();
	gconf_client_set_string(client, key, colorstr, NULL);
	g_free(colorstr);
}

void
on_fgColorPicker_color_set		(GnomeColorPicker *colorpicker,
					 guint r,
					 guint g,
					 guint b,
					 guint a,
					 gpointer user_data)
{
	real_color_set(current_keys.fg, r, g, b, a);
}

void
on_bgColorPicker_color_set		(GnomeColorPicker *colorpicker,
					 guint r,
					 guint g,
					 guint b,
					 guint a,
					 gpointer user_data)
{
	real_color_set(current_keys.bg, r, g, b, a);
}


void
on_IM_Tree_View_Selection_changed(GtkTreeSelection *selection,
		gpointer data)
{
	GtkTreeIter iter;
	GtkTreeModel *model;
	gchar *id = NULL;
	GtkWidget *w;

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &id,
				-1);
		g_stpcpy(curr_im_id, id);
		//fprintf(stderr, "get id : %s\n", id);
		
		
        w = glade_xml_get_widget(xml_prefedit, "buttonIMProperty");
		if(!strcmp(curr_im_id,"upim") || 
			!strcmp(curr_im_id,"itabc") || 
			//!strcmp(curr_im_id,"pinyin") || 
			!strcmp(curr_im_id,"wubizixing"))
		{
			gtk_widget_set_sensitive(GTK_WIDGET(w),TRUE);
		}else{
			gtk_widget_set_sensitive(GTK_WIDGET(w),FALSE);
		}
	}
}

void
on_IM_Button_Add_clicked (GtkButton *button, gpointer data)
{
	GtkWidget *w;
	GtkTreeSelection *selection;
	GConfClient *client = gconf_client_get_default();

	gconf_client_set_int(client,
			STATUS_COMMAND_KEY,
			REFRESH_NONE,
			NULL);

	im_all_list = IM_get_all_list();

	xml_IMAdd = glade_xml_new(UNIHAN_DATA_DIR "/xml/IMAdd.glade",
			"windowIMAdd", NULL);

	glade_xml_signal_autoconnect(xml_IMAdd);
	glade_xml_signal_connect(xml_IMAdd, "gtk_widget_destroy",
			G_CALLBACK(on_IM_All_Window_destroy));

	w = glade_xml_get_widget(xml_IMAdd, "treeviewIMAll");
	IM_treeview_add_column(GTK_TREE_VIEW(w));
	IM_refresh_treeview_by_im_list1(GTK_TREE_VIEW(w), im_all_list);
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
	g_signal_connect(selection,
			"changed",
			G_CALLBACK(on_IM_All_Tree_View_Selection_changed),
			NULL);

	w = glade_xml_get_widget(xml_IMAdd, "buttonIMAllAdd");
	//gtk_widget_set_sensitive(w, FALSE);
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_IM_All_Button_Add_clicked),
			NULL);

	w = glade_xml_get_widget(xml_IMAdd, "buttonIMAllCancel");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_IM_All_Button_Cancel_clicked),
			NULL);
}

void
on_IM_Button_Remove_clicked (GtkButton *button, gpointer data)
{
	GtkWidget *w;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GtkTreeModel *model;
	gchar *id, *id_temp1, *id_temp2;
	gchar *message;
	GtkWidget *dialog;
	GConfClient *client = gconf_client_get_default();

	gconf_client_set_int(client,
			STATUS_COMMAND_KEY,
			REFRESH_NONE,
			NULL);

	w = glade_xml_get_widget(xml_prefedit, "treeviewIM");
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &id,
				-1);
		if (strcmp(id, gconf_client_get_string(client,  DEFAULT_IM_KEY, NULL)) == 0)
		{
			message = "默认输入法不能删除！！！";
			dialog = gtk_message_dialog_new(NULL,
				GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
				GTK_MESSAGE_WARNING, 
				GTK_BUTTONS_OK, 
				message);
			gtk_dialog_run(GTK_DIALOG(dialog));
			gtk_widget_destroy(dialog);
			return;		
		}
		gconf_client_set_string(client,
				STATUS_IM_DEL_KEY,
				id,
				NULL);
		if (gtk_tree_model_iter_next(model, &iter)) {
			gtk_tree_model_get(model, &iter, STORE_2, &id_temp1, -1);
			g_stpcpy(curr_im_id, id_temp1); 
		} else {
			gtk_tree_model_get_iter_first(model, &iter);
			gtk_tree_model_get(model, &iter, STORE_2, &id_temp2, -1);
			g_stpcpy(id_temp1, id_temp2);
			while (gtk_tree_model_iter_next(model, &iter)) {
				gtk_tree_model_get(model, &iter, STORE_2, &id_temp2, -1);
				if (!strcmp(id, id_temp2)) {
					g_stpcpy(curr_im_id, id_temp1);
					return;
				} else {
					g_stpcpy(id_temp1, id_temp2);
				}
			}
		}
	}
	/*gconf_client_set_string(client,
			IM_DEL_ZERO,
			id,
			NULL);*/
}

void
on_IM_Button_Property_clicked (GtkButton *button, gpointer data)
{
//	GConfClient *client;
	//printf("curr_im_id:%s\n",curr_im_id);
	if(!strcmp(curr_im_id,"upim"))
	{
	     	 upimdpref_main();
	}else if(!strcmp(curr_im_id,"itabc"))
	{
	 	itabcpref_main();
	}else if(!strcmp(curr_im_id,"pinyin"))
	{
		pinyinpref_main();
	}else if(!strcmp(curr_im_id,"wubizixing"))
	{
		wubipref_main();
	}
	//else if (!strcmp(default_id,"upim")) upimdpref_main();
	//else if (!strcmp(default_id,"itabc"))itabcpref_main();
	//else if (!strcmp(default_id,"pinyin"))pinyinpref_main();
	//else if(!strcmp(default_id,"wubizixing"))wubipref_main();
	//else if(1) upimdpref_main();
	return;
}

void
on_IM_Button_ShiftUp_clicked (GtkButton *button, gpointer data)
{
	GtkWidget *w;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GtkTreeModel *model;
	gchar *id;
	GConfClient *client = gconf_client_get_default();

	gconf_client_set_int(client,
			STATUS_COMMAND_KEY,
			REFRESH_NONE,
			NULL);
	gconf_client_set_string(client,
			STATUS_IM_SHIFT_UP,
			SHIFT_UP,
			NULL);

	w = glade_xml_get_widget(xml_prefedit, "treeviewIM");
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &id,
				-1);
		gconf_client_set_string(client,
				STATUS_IM_SHIFT_UP,
				id,
				NULL);
	}
	return;
}

void
on_IM_Button_ShiftDown_clicked (GtkButton *button, gpointer data)
{
	GtkWidget *w;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GtkTreeModel *model;
	gchar *id;
	GConfClient *client = gconf_client_get_default();

	gconf_client_set_int(client,
			STATUS_COMMAND_KEY,
			REFRESH_NONE,
			NULL);
	gconf_client_set_string(client,
			STATUS_IM_SHIFT_DOWN,
			SHIFT_DOWN,
			NULL);

	w = glade_xml_get_widget(xml_prefedit, "treeviewIM");
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &id,
				-1);
		gconf_client_set_string(client,
				STATUS_IM_SHIFT_DOWN,
				id,
				NULL);
	}
	return;
}

void
on_Symbol_Tree_View_Selection_changed(GtkTreeSelection *selection,
		gpointer data)
{
	return;
}

void
on_Symbol_Button_Add_clicked (GtkButton *widget, gpointer data)
{
	GtkWidget *w;
	GtkTreeSelection *selection;
	GConfClient *client = gconf_client_get_default();

	xml_SymbolNew = glade_xml_new(UNIHAN_DATA_DIR "/xml/SymbolNew.glade",
			"windowSymbolNew", NULL);

	glade_xml_signal_autoconnect(xml_SymbolNew);
	glade_xml_signal_connect(xml_SymbolNew, "gtk_widget_destroy",
			G_CALLBACK(on_Symbol_new_Window_destroy));

	w = glade_xml_get_widget(xml_SymbolNew, "treeviewSymbolKind");
	Symbol_Kind_treeview_add_column(GTK_TREE_VIEW(w));
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
	gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
	g_signal_connect(GTK_TREE_SELECTION(selection),
			"changed",
			G_CALLBACK(on_Symbol_Kind_Tree_View_Selection_changed),
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "treeviewSymbolCands");
	Symbol_Cands_treeview_add_column(GTK_TREE_VIEW(w));
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
	gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
	g_signal_connect(GTK_TREE_SELECTION(selection),
			"changed",
			G_CALLBACK(on_Symbol_Cands_Tree_View_Selection_changed),
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "treeviewKeyCands");
	Key_Cands_treeview_add_column(GTK_TREE_VIEW(w));
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
	gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
	g_signal_connect(GTK_TREE_SELECTION(selection),
			"changed",
			G_CALLBACK(on_Key_Cands_Tree_View_Selection_changed),
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "treeviewSymbolNew");
	Symbol_new_treeview_add_column(GTK_TREE_VIEW(w));
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
	gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
	g_signal_connect(GTK_TREE_SELECTION(selection),
			"changed",
			G_CALLBACK(on_Symbol_new_Tree_View_Selection_changed),
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "buttonSymbolNewOK");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Symbol_new_Button_OK_clicked),
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "buttonSymbolNewCancel");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Symbol_new_Button_Cancel_clicked),
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "buttonSymbolNewAdd");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Symbol_new_Button_Add_clicked),
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "buttonSymbolNewDel");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Symbol_new_Button_Del_clicked),
			NULL);
	gconf_client_set_int(client,
			PREFEDIT_COMMAND_KEY,
			REFRESH_NONE,
			NULL);
}

void
on_Symbol_Button_Del_clicked(GtkButton *button, gpointer data)
{
	GtkWidget *treeview;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GtkTreeModel *model;
	gint idx;
	GConfClient *client = gconf_client_get_default();

	gconf_client_set_int(client,
			PREFEDIT_SYMBOL_DEL_KEY,
			-1,
			NULL);
	treeview = glade_xml_get_widget(xml_prefedit, "treeviewSymbol");
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &idx,
				-1);
	}
	else {
		warning_dialog(_("Please choose a symbol you want to delete!"));
		return;
	}

	gconf_client_set_int(client,
			PREFEDIT_SYMBOL_DEL_KEY,
			idx,
			NULL);
}

void
on_Symbol_Button_Apply_clicked(GtkButton *widget, gpointer data)
{
	return;
}

void
on_Word_Tree_View_Selection_changed(GtkTreeSelection *selection,
		gpointer data)
{
	GtkTreeIter iter;
	GtkTreeModel *model;
	gchar *string;

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &string,
				-1);
		fprintf(stderr, "get string : %s\n", string);
	}
}

void
on_Word_Button_Add_clicked(GtkButton *widget, gpointer data)
{
	GtkWidget *w;
	GtkTreeSelection *selection;
	GConfClient *client = gconf_client_get_default();

	xml_WordNew = glade_xml_new(UNIHAN_DATA_DIR "/xml/WordNew.glade",
			"windowWordNew", NULL);
	glade_xml_signal_autoconnect(xml_WordNew);
	glade_xml_signal_connect(xml_WordNew, "gtk_widget_destroy",
			G_CALLBACK(on_Word_new_Window_destroy));

	w = glade_xml_get_widget(xml_WordNew, "treeviewWordNew");
	Word_new_treeview_add_column(GTK_TREE_VIEW(w));
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
	g_signal_connect(selection,
			"changed",
			G_CALLBACK(on_Word_new_Tree_View_Selection_changed),
			NULL);

	w = glade_xml_get_widget(xml_WordNew, "buttonWordNewOK");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Word_new_Button_OK_clicked),
			NULL);

	w = glade_xml_get_widget(xml_WordNew, "buttonWordNewCancel");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Word_new_Button_Cancel_clicked),
			NULL);

	w = glade_xml_get_widget(xml_WordNew, "buttonWordNewAdd");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Word_new_Button_Add_clicked),
			NULL);

	w = glade_xml_get_widget(xml_WordNew, "buttonWordNewDel");
	g_signal_connect(GTK_BUTTON(w),
			"clicked",
			G_CALLBACK(on_Word_new_Button_Del_clicked),
			NULL);
	gconf_client_set_int(client,
			PREFEDIT_COMMAND_KEY,
			REFRESH_NONE,
			NULL);
}

void
on_Word_Button_Del_clicked(GtkButton *widget, gpointer data)
{
	GtkWidget *treeview;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GtkTreeModel *model;
	gchar *key;
	GConfClient *client = gconf_client_get_default();

	treeview = glade_xml_get_widget(xml_prefedit, "treeviewWord");
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
			STORE_1, &key,
			-1);
	}
	else {
		warning_dialog(_("Please choose a word you want to delete!"));
		return;
	}

	gconf_client_set_string(client,
			PREFEDIT_WORD_DEL_KEY,
			key,
			NULL);
}

void
on_Word_Button_Apply_clicked(GtkButton *widget, gpointer data)
{
	return;
}

void
clipboard_get_handler(GtkClipboard *cb, GtkSelectionData *sel_data,
		guint info, gpointer data)
{
	gchar *target = gdk_atom_name(sel_data->target);
	guint num;
	GList *l;
	gchar **str_list = NULL;
	gchar *final_str = NULL;
	int i;

	switch (info) {
		case TARGET_SYMBOL_LIST:
			num = g_list_length(symbol_list_new);
			l = symbol_list_new;

			str_list = g_new0(gchar *, num * 3 + 2);
			for (i=0; l; i++, l=g_list_next(l)) {
				SymDefGroup *pSDG = (SymDefGroup *)l->data;

				str_list[i*3 + 1] =
					g_strdup_printf("%u", pSDG->modifier);
				str_list[i*3 + 2] =
					g_strdup_printf("%u", pSDG->key);
				str_list[i*3 + 3] = pSDG->symbol;
			}
			str_list[0] = g_strdup_printf("%u", num);
			final_str = g_strjoinv("\n", str_list);

			gtk_selection_data_set(sel_data,
					gdk_atom_intern("UTF8_STRING", FALSE),
					8, final_str, strlen(final_str));
			i--;
			for (; i>=0; i--) {
				g_free(str_list[i*3 + 1]);
				g_free(str_list[i*3 + 2]);
			}
			g_free(str_list[0]);
			g_free(str_list);
			g_free(final_str);
			break;
		case TARGET_WORD_LIST:
			num = g_list_length(word_list_new);
			l = word_list_new;

			str_list = g_new0(gchar *, num*2 + 2);
			for (i=0; l; i++, l=g_list_next(l)) {
				WordDefGroup *pWDG = (WordDefGroup *)l->data;

				str_list[i*2 + 1] = pWDG->key;
				str_list[i*2 + 2] = pWDG->word;
			}
			str_list[0] = g_strdup_printf("%u", num);
			final_str = g_strjoinv("\n", str_list);

			gtk_selection_data_set(sel_data,
					gdk_atom_intern("UTF8_STRING", FALSE),
					8, final_str, strlen(final_str));
			g_free(str_list[0]);
			g_free(str_list);
			g_free(final_str);
			break;
	}
	g_free(target);
}

void
on_Status_Command_toggled(GConfClient *client,
		guint cnxn_id,
		GConfEntry *entry,
		gpointer data)
{
	GtkWidget *w;
	gint command = 3;
	GConfValue *value = gconf_entry_get_value(entry);
	if (value != NULL)
		command = gconf_value_get_int(value);

	switch (command) {
		case REFRESH_IM_LIST:
			IM_free_list(im_curr_list);
			im_curr_list = NULL;
			im_curr_list = IM_get_curr_list();
			w = glade_xml_get_widget(xml_prefedit, "treeviewIM");
			IM_refresh_treeview_by_im_list(GTK_TREE_VIEW(w),
					im_curr_list);
			IM_treeview_set_selection(GTK_TREE_VIEW(w), curr_im_id);
			refresh_im_default();
			//model->imCurrList = im_curr_list;
			break;
		case REFRESH_SYMBOL_LIST:
			Symbol_free_list(symbol_list);
			symbol_list = NULL;
			Symbol_free_list(symbol_list_new);
			symbol_list_new = NULL;
			symbol_list = Symbol_get_list();
			w = glade_xml_get_widget(xml_prefedit,
					"treeviewSymbol");
			Symbol_refresh_treeview_by_symbol_list(GTK_TREE_VIEW(w),
					symbol_list);
			break;
		case REFRESH_WORD_LIST:
			Word_free_list(word_list);
			word_list = NULL;
			Word_free_list(word_list_new);
			word_list_new = NULL;
			word_list = Word_get_list();
			w = glade_xml_get_widget(xml_prefedit, "treeviewWord");
			Word_refresh_treeview_by_word_list(GTK_TREE_VIEW(w),
					word_list);
			break;
		default:
			return;
	}
}

void
on_help_button_clicked(GtkButton *widget, gpointer data)
{
	GError* err;
	
	if(!gnome_help_display ("ht.xml", NULL, &err))
    	printf("call on_help_button_clicked failure:%s.\n", err->message);
}


void
on_phraseCheck_toggled                 (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	GConfClient *client = gconf_client_get_default();
	gconf_client_set_bool(client, PHRASE_MODE_KEY, gtk_toggle_button_get_active(togglebutton), NULL);
}

void
on_gsuggestCheck_toggled               (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
	GConfClient *client = gconf_client_get_default();
	gconf_client_set_bool(client, USE_GOOGLE_SUGGEST_KEY, gtk_toggle_button_get_active(togglebutton), NULL);
}

// vi:ts=4:nowrap:cin:expandtab
